//
//  MCImageView.m
//  MClient
//
//  Created by 崔玉国 on 10-9-20.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MCImageView.h"
#import "UIImageAdditions.h"
//////////////////////////////////////////////////////////////////////////////////////////////////
@implementation MCSize
@synthesize width, height;
@synthesize filename;
- (void)dealloc {
	[filename release];
	[super dealloc];
}
@end

@implementation MCImageView
//////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject

#define SafeRelease(p) if (p) {\
                        [p release]; p = nil;\
}

@synthesize imgw, imgh;


- (id)initWithFrame:(CGRect)frame {
	if (self = [super initWithFrame:frame]) {
		_resivingData = NO;
		_activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake((frame.size.width-32)/2, (frame.size.height-40)/2, 32, 32)];
		[_activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
		[self addSubview:_activityIndicator];
		self.contentMode = UIViewContentModeCenter;// | UIViewContentModeScaleToFill;
		//self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;//nTableCellAutoresizingMask;
	}
	return self;
}

- (void)dealloc {
	[[NSNotificationCenter defaultCenter] postNotificationName:@"viewControllerDealloc" object:self];
	[_streamDao release];
	[_activityIndicator release];
	[fileName release];
	SafeRelease(_cacheFileName);
	if (_cacheFileName) {
		NSFileManager *fileManager = [NSFileManager defaultManager];
		[fileManager removeItemAtPath:_cacheFileName error:nil];
	}
	[super dealloc];
}

//////////////////////////////////////////////////////////////////////////////////////////////////
// UIView

//- (void)drawRect:(CGRect)rect {
//	[_image drawInRect:rect];
//}

-(NSMutableDictionary *)fileAttribute
{
	return nil;
}

-(void)setFileAttribute:(NSMutableDictionary *)fileAttribute
{
	if (self.image || _resivingData) {
		return;
	}
	NSString* fileNm = [fileAttribute objectForKey:@"fileName"];
	if (nil == fileNm || [fileNm length] == 0) {
		return;
	}

	//attribute = [fileAttribute retain];
	fileName = [[NSString stringWithFormat:@"indexPath_Seciton%@_Row%@_%@", [fileAttribute objectForKey:@"indexPath_section"],
				 [fileAttribute objectForKey:@"indexPath_row"], fileNm] retain];
	
	NSString *cacheFile = [MCFunction getTempFileName:[[fileNm pathExtension] lowercaseString]];
	_cacheFileName = [[NSString stringWithFormat:@"%@random%d%@", NSTemporaryDirectory(), arc4random(), cacheFile] retain];

	NSFileManager *fileManager = [NSFileManager defaultManager];
	[fileManager changeCurrentDirectoryPath:[NSTemporaryDirectory() stringByExpandingTildeInPath]];
	[fileManager createFileAtPath:_cacheFileName contents:nil attributes:nil];

	_resivingData = YES;
	SafeRelease(_streamDao);
	_streamDao = [[MServerStreamDAO alloc] init];
	[_streamDao downloadFileAsync:[fileAttribute objectForKey:@"actionType"]
						 dataFrom:[fileAttribute objectForKey:@"dataFrom"]
						 location:[fileAttribute objectForKey:@"fileLocation"]
						 fileName:fileNm
							 page:0
							 type:[fileAttribute objectForKey:@"type"] == nil?@"":[fileAttribute objectForKey:@"type"] 
					 saveLocation:_cacheFileName 
						 delegate:self];
	MClientAppDelegate *appDelegate = (MClientAppDelegate*)[[UIApplication sharedApplication] delegate];
	[appDelegate stopActivityIndicatorView];
	[_activityIndicator startAnimating];
}


- (void) getDataFromServerSuccess: (NSMutableDictionary *)returnValue {
	
	//NSLog(@"cacke---%@", _cacheFileName);
	self.image = [UIImage imageWithContentsOfFile:_cacheFileName];

	if (self.image != nil) {
		if (imgw > 0 && imgh > 0) {
			self.image = [MCFunction scaleAndRotateImage:[UIImage imageWithContentsOfFile:_cacheFileName]
												   width:imgw
												  height:imgh];
		} 
		
		CGSize imgSize = [self.image size];
		MCSize* size = [[MCSize alloc] init];
		size.width = imgSize.width;
		size.height = imgSize.height;
		size.filename = fileName;
		
		imgw = imgSize.width;
		imgh = imgSize.height;
		
		//NSLog(@"width=%d,height=%d", size.width, size.height);
		//NSLog(@"filename=%@, fileLocation=%@, actionType=%@, dataFrom=%@", [attribute objectForKey:@"fileName"],
		//	  [attribute objectForKey:@"fileLocation"], [attribute objectForKey:@"actionType"], [attribute objectForKey:@"dataFrom"]);
		
		[[NSNotificationCenter defaultCenter] postNotificationName:@"ImageSize" object:size];
	}

	[_activityIndicator stopAnimating];
	_resivingData = YES;
}

- (void)getServerError: (NSString *)returnCode returnMessage:(NSString *)returnMessage
{
	[_activityIndicator stopAnimating];
	_resivingData = YES;
}

@end

